from forms.ui_create_project import Ui_Dialog
from PyQt5 import QtWidgets
import os
from PyQt5.QtCore import pyqtSignal
import shutil

class DialogCreateProject(QtWidgets.QDialog, Ui_Dialog):
    # 创建成功的信号
    create_success_signal = pyqtSignal(str, str, str, bool)

    def __init__(self, parent=None, default_directory='', is_default=False, is_save_as=False):
        super(DialogCreateProject, self).__init__(parent)
        self.setupUi(self)

        # 初始化
        self.default_directory = default_directory
        self.is_save_as = is_save_as
        self.is_default = is_default

        # 设置默认的项目路径
        self.lineEdit_2.setText(self.default_directory)
        self.checkBox.setChecked(self.is_default)
        
        # 将默认的ok与cancel文本修改为新建与取消
        self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setText('确定')
        self.buttonBox.button(QtWidgets.QDialogButtonBox.Cancel).setText('取消')

        # 信号与槽的连接
        self.pushButton.clicked.connect(self.choose_project_directory)
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

    def choose_project_directory(self):
        directory = QtWidgets.QFileDialog.getExistingDirectory(self, "选择项目路径", self.lineEdit_2.text())
        if directory:
            self.lineEdit_2.setText(directory)

    def accept(self):
        # 获取输入框中的数据
        project_name = self.lineEdit.text()
        defaultDirectory = self.lineEdit_2.text()
        is_default = self.checkBox.isChecked()

        if not project_name:
            QtWidgets.QMessageBox.warning(self, '警告', '项目名称不能为空！')
            return
        if not os.path.exists(defaultDirectory):
            QtWidgets.QMessageBox.warning(self, '警告', '项目路径不存在！')
            return 
        
        # 创建一个新的文件目录
        project_path = os.path.join(defaultDirectory, project_name)
        if not os.path.exists(project_path):
            os.makedirs(project_path)
        
        if self.is_save_as:
            # 保存为功能
            self.copy_directory(self.default_directory, project_path)
        else:
            # 创建默认的sql.json文件
            self.create_default_sql_json(project_path)

            # 创建默认的node.xml文件
            self.create_default_node_xml(project_path)

        # 发送信号，通知主窗口更新项目列表
        self.create_success_signal.emit(project_name,project_path, defaultDirectory, is_default)

        # 关闭对话框
        super().accept()
    
    def create_default_sql_json(self, project_path):
        # 创建默认的sql.json文件
        sql_json_path = os.path.join(project_path, 'sql_data.json')
        if not os.path.exists(sql_json_path):
            with open(sql_json_path, 'w', encoding='utf-8') as f:
                f.write('{}')

    def create_default_node_xml(self, project_path):
        # 创建默认的node.xml文件
        node_xml_path = os.path.join(project_path, 'node_data.xml')
        if not os.path.exists(node_xml_path):
            with open(node_xml_path, 'w', encoding='utf-8') as f:
                f.write('<root></root>')

    def copy_directory(self, src, dst):
        if not os.path.exists(dst):
            os.makedirs(dst)

        for item in os.listdir(src):
            s = os.path.join(src, item)
            d = os.path.join(dst, item)
            if os.path.isfile(s):
                shutil.copy2(s, d)
    
    